package treedemo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 87111
 * Date: 2025-09-10
 * Time: 12:54
 */
public class BinaryTree {

    static class TreeNode {
        public char val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(char val) {
            this.val = val;
        }
    }
    private TreeNode root;

    //创建一棵二叉树
    public TreeNode createTree() {
        TreeNode node1 = new TreeNode('A');
        TreeNode node2 = new TreeNode('B');
        TreeNode node3 = new TreeNode('C');
        TreeNode node4 = new TreeNode('D');
        TreeNode node5 = new TreeNode('E');
        TreeNode node6 = new TreeNode('F');
        TreeNode node7 = new TreeNode('G');
        TreeNode node8 = new TreeNode('H');

        root = node1;
        root.left = node2;
        root.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;
        node5.right = node8;

        return root;
    }

    //前序遍历
    public void preOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        System.out.print(root.val + " ");
        preOrder(root.left);
        preOrder(root.right);
    }
    //非递归实现前序遍历
    public void preOrderNor() {
        if(root == null) {
            return;
        }
    }

    //中序遍历
    public void inOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        inOrder(root.left);
        System.out.print(root.val + " ");
        inOrder(root.right);
    }

    //后序遍历
    public void postOrder(TreeNode root) {
        if(root == null) {
            return;
        }
        postOrder(root.left);
        postOrder(root.right);
        System.out.print(root.val + " ");
    }

    //求二叉树的节点个数(设置一个变量计数）
    public int TreeSize = 0;
    public void size(TreeNode root) {
        if(root == null) {
            return;
        }
        TreeSize++;
        size(root.left);
        size(root.right);
    }
    //求二叉树节点数 = 左子树节点数+右子树节点数+1
    public int size1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        return size1(root.left) + size1(root.right) + 1;
    }

    //求叶子节点的个数(设置一个变量计数)
    public int leafSize = 0;
    public void getLeafNodeNum(TreeNode root) {
        if(root == null) {
            return;
        }
        if(root.left == null && root.right == null) {
            leafSize++;
        }
        getLeafNodeNum(root.left);
        getLeafNodeNum(root.right);
    }
    //叶子节点数 = 左树的叶子节点数+右树的叶子节点数
    public int getLeafNodeNum1(TreeNode root) {
        if(root == null) {
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }
        return getLeafNodeNum1(root.left) + getLeafNodeNum1(root.right);
    }

    //获取第K层节点个数(整棵树的第K层 = 左子树的第K-1层 + 右子树的第K-1层）
    public int getKLevelNodeNum(TreeNode root, int K) {
        if(root == null) {
            return 0;
        }
        if(K == 1) {
            return 1;
        }
        return getKLevelNodeNum(root.left,K-1) + getKLevelNodeNum(root.right,K-1);
    }

    //获取二叉树的高度(左子树的高度和右子树的高度取最大值 + 1)
    public int getTreeHeight(TreeNode root) {
        if(root == null) {
            return 0;
        }
        int leftHeight = getTreeHeight(root.left);
        int rightHeight = getTreeHeight(root.right);
        return Math.max(leftHeight,rightHeight) + 1;
    }

    //检查值为value元素是否存在
    public TreeNode find(TreeNode root, char val) {
        if(root == null) {
            return null;
        }
        if(root.val == val) {
            return root;
        }
        //判断左树是否找到
        TreeNode ret = find(root.left,val);
        if(ret.val == val) {
            return ret;
        }
        //判断右树是否找到
        ret = find(root.right,val);
        if(ret.val == val) {
            return ret;
        }
        return null;
    }

}
